// components/recipe-status/index.js
import {
  request
} from '../../api/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    status: {
      type: String,
      value: '',
      observer: 'setDataSource'
    },
    recipe: {
      type: Object,
      value: {}
    },
    root: {
      type: Boolean,
      observer: 'check'
    },
    show: {
      type: Boolean,
      observer: 'check'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    value: null,
    isRoot: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setDataSource(value) {
      this.setData({
        value
      })
    },
    check(val) {
        this.setData({
          isRoot: getApp().globalData.root
        })
    },
    onChange() {
      if(!getApp().globalData.root) {
        return 
      }
      const value = this.data.value;
      let target, text;
      switch (value) {
        case 'TO_BE_ANNOUNCED':
          target = 'ANNOUNCED';
          text = '已发布';
          break;
        case 'ANNOUNCED':
          target = 'STOP';
          text = '已挂起';
          break;
        case 'STOP':
          target = 'ANNOUNCED';
          text = '已发布';
          break;
        default:
          wx.showToast({
            title: '修改失败',
            icon: 'error'
          })
          break;
      }
      if (target) {
        const _this = this;
        wx.showModal({
          title: '提示',
          content: `原料配方状态将被修改成： ${text}`,
          success(res) {
            if (res.confirm) {
              request.ingredientsUpdateStatus({
                id: _this.properties.recipe?.id,
                status: target
              }, () => {
                _this.setData({
                  value: target
                })
                _this.triggerEvent('change',target);
              })
            }
          }
        })
      }
    }
  }
})